//
//  JKNewsListCell.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/2.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKNewsListCell: JKBaseTableViewCell {

    @IBOutlet private weak var titleLabel: UILabel!
    @IBOutlet weak var subLabel: UILabel!
    @IBOutlet private weak var rightImageView: UIImageView!
    @IBOutlet private weak var multiPicView: UIView!
    @IBOutlet private weak var imageCoverView: UIView!
    @IBOutlet private weak var sepView: UIView!
    
    @IBOutlet weak var titleLabelRightCons: NSLayoutConstraint!
    
    @IBOutlet weak var multiPicContainerView: UIView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
//        contentView.backgroundColor = JKRandomColor()
        titleLabel.font = JKCellTitleFont
    }
    
    override func initialization() {
        super.initialization()
        
    }
    
    var model: JKNewsListModel? {
        
        didSet{
            
            self.titleLabel.textColor = (skinModel == JKNightSkinModelValue) ?
                ((self.model?.alreadyRead ?? false) ? JKColor(128, 128, 128) : JKColor(186, 186, 186)) :
                ((self.model?.alreadyRead ?? false) ? JKColor(153, 153, 153) : JKColor(53, 53, 53))
            
            multiPicView.isHidden = !(model?.multipic)!
            titleLabel.text = model?.title
            
            titleLabelRightCons.constant = (model?.images == nil) ? -70 : 18
            
            subLabel.isHidden = model?.display_date == nil
            subLabel.text = model?.display_date
            
            guard let _ = model?.images else {
                
                rightImageView.isHidden = true
                
                self.imageCoverView.isHidden = (skinModel == JKDaySkinModelValue) || rightImageView.isHidden
                return
            }
            rightImageView.isHidden = false
            
            self.imageCoverView.isHidden = (skinModel == JKDaySkinModelValue) || rightImageView.isHidden
            
            rightImageView.jk_setImage(withUrlStr: model?.images?[0], placeHolder: "Image_Preview")
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        
        imageCoverView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        multiPicContainerView.backgroundColor = UIColor.black
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        imageCoverView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        multiPicContainerView.backgroundColor = UIColor.black
        
        updateSkin()
    }
}

extension JKNewsListCell {
    
    override func updateSkin() {
        super.updateSkin()
        
        self.titleLabel.textColor = (skinModel == JKNightSkinModelValue) ?
            ((self.model?.alreadyRead ?? false) ? JKColor(128, 128, 128) : JKColor(186, 186, 186)) :
            ((self.model?.alreadyRead ?? false) ? JKColor(153, 153, 153) : JKColor(53, 53, 53))
        self.subLabel.textColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(108) : JKSameRGBColor(118)
        
        self.imageCoverView.isHidden = (skinModel == JKDaySkinModelValue) || rightImageView.isHidden
        
        if isSelected || isHighlighted {
            
            self.sepView.backgroundColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(118) : JKSameRGBColor(153)
            
            return
        }
        
        self.sepView.backgroundColor = (skinModel == JKNightSkinModelValue) ? JKSameRGBColor(66) : JKSameRGBColor(217)
    }
}
